<?php
/**
 * 报价
 * @author yu
 * @created 2017-2-14 14:03:42
 */
namespace offer\service;

use common\service\CommonService;
use herosphp\core\Loader;
use herosphp\core\Log;

class OfferService extends CommonService
{


    public function __construct()
    {
        parent::__construct('offer');
    }

    /**
     * 获取报价列表
     */
    public function getOfferList($pagesize, $page, $search_arr)
    {

        $offerModel = Loader::model('offer');

        if ($search_arr['start_time'] !== '' && $search_arr['start_time'] !== NULL) {
            $conditions['addtime'] = array();
            $conditions['addtime']['>'] = $search_arr['start_time'] . ' 00:00:00';
        }
        if ($search_arr['end_time'] !== '' && $search_arr['end_time'] !== NULL) {
            if (!$conditions['addtime']) {
                $conditions['addtime'] = array();
            }
            $conditions['addtime']['<='] = $search_arr['end_time'] . ' 23:59:59';
        }
        if ($search_arr['saas_user_id'] != '') {

            $conditions['saas_user_id'] = $search_arr['saas_user_id'];
        }

        $result['items'] = $offerModel->getItems($conditions, '*', array('addtime' => -1), array($page, $pagesize));
        $result['total'] = $offerModel->count($conditions);

        foreach ($result['items'] As $k => $item) {


            $offerProducts = $this->getofferProducts($item['id']);
            //用报价单产品列表中的第一个产品属性拼接标题
            $result['items'][$k]['title'] = $offerProducts[0]['product_name'] . '/' . $offerProducts[0]['factory_name'] . '/' . $offerProducts[0]['brand_name'];
            $result['items'][$k]['product_counts'] = $this->getofferProductsCounts($item['id']);

        }

        return $result;

    }

    /**
     * 根据报价单id获取报价数据
     * @param $offerId
     * @return mixed
     */
    public function getOffer($offerId, $saas_user_id = '')
    {

        $offerProductModel = Loader::model('offer');

        if ($saas_user_id == '') {

            $item = $offerProductModel->getItem($offerId);

        } else {

            $item = $offerProductModel->getItem(array('id' => $offerId, 'saas_user_id' => $saas_user_id));
        }

        return $item;

    }

    /**
     * 获取该用户上一次报价的联系人信息
     * @param $saas_user_id
     */
    public function getLastOfferContactInfo($saas_user_id)
    {

        $offerProductModel = Loader::model('offer');

        $item = $offerProductModel->getItem(array('saas_user_id' => $saas_user_id), null, array('addtime' => -1), 1);

        return $item;

    }

    /**
     * 根据报价单id,增加浏览量
     * @param $offerId
     * @param $increment 增量
     * @return mixed
     */
    public function plusOfferView($offerId, $increment = 1)
    {

        $offerProductModel = Loader::model('offer');

        return $offerProductModel->increase('views', $increment, $offerId);

    }

    /**
     * 根据当前时间秒值来取模，来获取随机数或者取1，作为增量
     * @param $mod 模数
     * @return int
     */
    public function getRandIncrement($mod=3)
    {
        $s = date('s');
        if ($s % $mod==0) {
            return rand(0, 3);
        } else {
            return 1;
        }
    }

    /**
     * 根据报价单id获取报价产品数量
     * @param $offerId
     * @return mixed
     */
    public function getofferProductsCounts($offerId)
    {


        $offerProductModel = Loader::model('offerProduct');

        $counts = $offerProductModel->count(array('offer_id' => $offerId));

        return $counts;

    }

    /**
     * 根据报价单id获取报价产品列表
     * @param $offerId
     * @return mixed
     */
    public function getofferProducts($offerId)
    {

        $offerProductModel = Loader::model('offerProduct');

        $items = $offerProductModel->getItems(array('offer_id' => $offerId));

        return $items;

    }


    /**
     * 添加报价信息
     * @param $offerHead 报价基本信息
     * @param $offerProducts 报价产品信息
     */
    public function addAll($offerHead, $offerProducts)
    {
        $offerModel = Loader::model('offer');

        $offerProductModel = Loader::model('offerProduct');

        $offerModel->beginTransaction();

        $offer_id = $offerModel->add($offerHead);

        foreach ($offerProducts as $product) {
            $product['offer_id'] = $offer_id;
            $product['addtime'] = myNow();
            $product['edittime'] = myNow();
            $product['is_delete'] = 0;

            if (!$offerProductModel->add($product)) {

                $offerModel->rollback();
                return false;
            }

        }

        $offerModel->commit();

        return $offer_id;

    }

    /**
     * @param $conditions
     * @param $fields
     * @return mixed
     */
    public function sum($conditions, $fields)
    {
        $db = $this->getDB();
        $where = " 1 ";
        if (is_string($conditions)) {
            $where .= " AND " . $conditions;
        }
        if (is_array($conditions)) {
            foreach ($conditions as $key => $value) {
                $where .= " AND {$key}='$value'";
            }
        }
        $sql = "select sum( `{$fields}` ) as `{$fields}` from offer where " . $where;

        $res = $db->query($sql);
        if ($res) {
            return $res[0][$fields];
        } else {
            return false;
        }

    }


}